<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Person extends CI_Controller {

	function __construct() {
		parent::__construct();

		$this -> load -> library('show_test');
		$this -> load -> library('session');
		$this -> load -> library('competence_lib');
		
		
		$this -> load -> model('client_model');
		$this -> load -> model('users_model');
		$this -> load -> model('insert_model');
		$this -> load -> model('importance_of_customers_model');

		$this -> load -> model('demand_model');
		$this -> load -> model('object_usage_model');
		$this -> load -> model("client/contact", 'contact');
	}
/*
	public function o_owner_c_name() {

		$oq = $this -> demand_model -> get_has_client_id();
		$c_name;

		foreach ($oq->result() as $row) {

			//echo $row->client_id;
			$c_name = '';
			if ($row -> client_id > 0) {

				$c_info = $this -> client_model -> get_client2($row -> client_id);
				foreach ($c_info->result() as $c_data) {
					$c_name = $c_data -> name;
				}
				//echo $c_info[0]->name;
			}

			if ($row -> client_id > 0 AND empty($row -> owner)) {
				//update_demand($data, $id);
				$this -> demand_model -> update_demand(array("name" => $c_name), $row -> demand_id);

			}

		}

	}
*/


	public function crid_josn() {

		//$this->o_owner_c_name();

		$page = isset($_POST['page']) ? $_POST['page'] : 1;
		$rp = isset($_POST['rp']) ? $_POST['rp'] : 60;
		$sortname = isset($_POST['sortname']) ? $_POST['sortname'] : 'demand_id';
		$sortorder = isset($_POST['sortorder']) ? $_POST['sortorder'] : 'desc';

		$query = isset($_POST['query']) ? $_POST['query'] : false;
		$qtype = isset($_POST['qtype']) ? $_POST['qtype'] : false;
		/////////////////////////////////////////////////////////////
		$name = isset($_POST['name']) ? $_POST['name'] : false;

		$required_level = isset($_POST['required_level']) ? $_POST['required_level'] : false;
		$object_usage = isset($_POST['object_usage']) ? $_POST['object_usage'] : false;

		$building_area1 = isset($_POST['building_area1']) ? $_POST['building_area1'] : false;
		$building_area2 = isset($_POST['building_area2']) ? $_POST['building_area2'] : false;

		$indoor_area1 = isset($_POST['indoor_area1']) ? $_POST['indoor_area1'] : false;
		$indoor_area2 = isset($_POST['indoor_area2']) ? $_POST['indoor_area2'] : false;

		$land_area1 = isset($_POST['land_area1']) ? $_POST['land_area1'] : false;
		$land_area2 = isset($_POST['land_area2']) ? $_POST['land_area2'] : false;

		$total_budget1 = isset($_POST['total_budget1']) ? $_POST['total_budget1'] : false;
		$total_budget2 = isset($_POST['total_budget2']) ? $_POST['total_budget2'] : false;

		$key_areas = isset($_POST['key_areas']) ? $_POST['key_areas'] : false;
		$specify_building = isset($_POST['specify_building']) ? $_POST['specify_building'] : false;

		$object_name = isset($_POST['object_name']) ? $_POST['object_name'] : false;

		$sections1 = isset($_POST['sections1']) ? $_POST['sections1'] : false;
		$sections2 = isset($_POST['sections2']) ? $_POST['sections2'] : false;

		$type_sell = isset($_POST['type_sell']) ? $_POST['type_sell'] : false;
		$type_rent = isset($_POST['type_rent']) ? $_POST['type_rent'] : false;

		$reload_type = isset($_POST['reload_type']) ? $_POST['reload_type'] : false;
		$object_case_type = isset($_POST['object_case_type']) ? $_POST['object_case_type'] : false;

		$administrative_area = isset($_POST['administrative_area']) ? $_POST['administrative_area'] : false;
		$year_of_construction1 = isset($_POST['year_of_construction1']) ? $_POST['year_of_construction1'] : false;
		$year_of_construction2 = isset($_POST['year_of_construction2']) ? $_POST['year_of_construction2'] : false;

		$keywords = isset($_POST['keywords']) ? $_POST['keywords'] : false;
		$broker = isset($_POST['broker']) ? $_POST['broker'] : false;
		$customer_source = isset($_POST['customer_source']) ? $_POST['customer_source'] : false;
		$ectime = isset($_POST['ectime']) ? $_POST['ectime'] : false;
		$sctime = isset($_POST['sctime']) ? $_POST['sctime'] : false;
		$status = isset($_POST['status']) ? $_POST['status'] : false;

		$query_type = isset($_POST['query_type']) ? $_POST['query_type'] : false;
		/*

		 var year_of_construction1 = $("#year_of_construction1").val();
		 var year_of_construction2 = $("#year_of_construction2").val();
		 */

		/////////////////////////////////////////////////////////////
		$where = '';
		$where_flag = TRUE;

		//重點區域
		if ($administrative_area) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `administrative_area1` LIKE '%" . $administrative_area . "%' ";
		}

		if ($sctime OR $ectime) {

			if ($sctime) {
				if ($where_flag) {
					$where .= " WHERE ";
					$where_flag = false;
				} else {
					$where .= " AND ";
				}

				$where .= "  `ctime` >= '" . (strtotime($sctime)-(60*60*24) ) . "' ";
			}

			if ($ectime) {

				if ($where_flag) {
					$where .= " WHERE ";
					$where_flag = false;
				} else {
					$where .= " AND ";
				}

				$where .= "  `ctime` <= '" . (strtotime($ectime)+(60*60*24)) . "' ";
			}
		}

		if ($customer_source) {
			$c_s_s=explode('/', $customer_source);

			foreach ($c_s_s as $_key => $_va) {								
				if(strlen($_va)>=2){
					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " OR ";
					}

					$where .= "  `customer_source` like '%" . $_va . "%' ";
				}
			}
		}

		if ($broker) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `broker` = '" . $broker . "' ";
		}

		if ($keywords) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}

			$where .= "  ( `park1` LIKE '%" . $keywords . "%' ";
			$where .= " OR `park2` LIKE '%" . $keywords . "%' ";
			$where .= " OR `circle1` LIKE '%" . $keywords . "%' ";
			$where .= " OR `circle2` LIKE '%" . $keywords . "%' ";
			$where .= " OR `administrative_area1` LIKE '%" . $keywords . "%' ";
			$where .= " OR `administrative_area2` LIKE '%" . $keywords . "%' )";
		}

		if ($status) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `status` LIKE '%" . $status . "%' ";
		}

		if ($year_of_construction1 OR $year_of_construction2) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			if ($year_of_construction1)
				$where .= "  `year_of_construction`  >= '" . $year_of_construction1 . "' ";

			if ($year_of_construction2)
				$where .= "  AND `year_of_construction`  <= '" . $year_of_construction2 . "' ";
		}

		if ($object_case_type) {

			$obj_spilt_str = explode(',', $object_case_type);
			$QUERY_str = '';
			$QUERY_str_flag = true;

			foreach ($obj_spilt_str as $__k => $__v) {

				if (!empty($__v)) {

					if ($where_flag) {
						$where .= " WHERE ";
						$where_flag = false;
					} else {
						$where .= " OR ";
					}

					$where .= "  `object_case_type` like '%" . $__v . "%' ";
				}
			}
		}

		if ($type_sell) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `type_sell` LIKE '%" . $type_sell . "%' ";
		}

		if ($type_rent) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `type_rent` LIKE '%" . $type_rent . "%' ";
		}

		if ($sections1) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `sections1` LIKE '%" . $sections1 . "%' ";
		}

		if ($sections2) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `sections2` LIKE '%" . $sections2 . "%' ";
		}

		if ($object_name) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `object_name1` LIKE '%" . $object_name . "%' ";

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}

			$where .= "  `object_name2` LIKE '%" . $object_name . "%' ";
		}

		if ($key_areas) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " OR ";
			}
			$where .= "  `key_areas1` LIKE '%" . $key_areas . "%' ";

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " OR ";
			}

			$where .= "  `key_areas2` LIKE '%" . $key_areas . "%' ";
		}

		if ($specify_building) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " OR ";
			}
			$where .= "  `specify_building1` LIKE '%" . $specify_building . "%' ";

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " OR ";
			}

			$where .= "  `specify_building2` LIKE '%" . $specify_building . "%' ";
		}

		if ($total_budget1) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `total_budget1` >= '" . $total_budget1 . "' ";
		}

		if ($total_budget2) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `total_budget2` <= '" . $total_budget2 . "' ";
		}

		if ($land_area1) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `land_area1` >= '" . $land_area1 . "' ";
		}

		if ($land_area2) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `land_area2` <= '" . $land_area2 . "' ";
		}

		if ($indoor_area1) {

			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `indoor_area1` >= '" . $indoor_area1 . "' ";
		}

		if ($indoor_area2) {
			if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
			$where .= "  `indoor_area2` <= '" . $indoor_area2 . "' ";
		}
		if ($building_area1) {

			if ($where_flag) {

				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}

			$where .= "  `building_area1` >= '" . $building_area1 . "' ";
		}

		if ($building_area2) {

			if ($where_flag) {

				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}

			$where .= "  `building_area2` <= '" . $building_area2 . "' ";
		}
		if ($object_usage) {

			$ous=explode('/', $object_usage);

			foreach ($ous as $key => $_value) {
				if(strlen($_value)>3 ){
				# code...			
					if ($where_flag) {

						$where .= " WHERE ";
						$where_flag = false;

					} else {
						$where .= " OR ";
					}

					$where .= "  `object_usage` LIKE '%" . trim($_value) . "%' ";
				}
			}
		}

		if ($required_level) {

			if ($where_flag) {

				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}

			$where .= "  `required_level` LIKE '%" . $required_level . "%' ";
		}

		if ($name) {

			if ($where_flag) {

				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}

			$where .= "  `name` LIKE '%" . $name . "%' ";
		}
		
		
//是否經紀人        
        $CI = &get_instance();
        $purview = $CI->login_lib->purview();
        $users_id = $CI->login_lib->users_id();

        if ($purview!='#all') {

            if ($where_flag) {
				$where .= " WHERE ";
				$where_flag = false;
			} else {
				$where .= " AND ";
			}
	
			$where .= "  `broker`   = '" . $users_id . "' ";
        }
		
		
		

		
		
		
		//$sql_data = array('SQL' => $where, 'type' => 'person/OBJECT/' . $reload_type);

		if (!empty($where)) {
			//$this->insert_model->insert( 'h_sql',$sql_data);
		}

		$impo = $this -> importance_of_customers_model -> get_all();
		$impo_s = array();

		foreach ($impo as $key => $value) {
			$impo_s[$value -> importance_of_customers_id] = $value -> value;
		}

		$query = $this -> demand_model -> demand_for_crid($page, $rp, $sortname, $sortorder, $where);

		header("Content-type: application/json");

		$object_usages = $this -> object_usage_model -> get_all();

		$object_usage_array = array();

		foreach ($object_usages as $_n => $_v) {
			$object_usage_array[$_v -> object_usage_id] = $_v -> value;
		}

		$total_rows = $this -> demand_model -> countRec("h_demand", $where);

		$jsonData = array('page' => $page, 'total' => $total_rows, 'rows' => array());
		$type = '';
		$object_usage_title = '';

		$client_data = $this -> get_client_contact_data('client');
		$contact_data = $this -> get_client_contact_data('contact');
		$all_clients = $this -> get_client_all_info();
		$all_usres = $this -> get_users();
		
		$all_se =   $this->session->all_userdata();

		foreach ($query->result() as $row) {
			$client_s_name = '';

			//demand的資料的client_ID
			//去對應到cleint的資料庫～
			//看有沒有對到
			//特徵
			$recommended = '';
			$broker_name = '';

			foreach ($all_usres as $u_id => $name) {

				if ($u_id == $row -> broker) {

					$broker_name = $name;
				}
			}

			if (!empty($row -> client_id)) {
				foreach ($all_clients as $client) {
					if ($client -> client_id == $row -> client_id) {
						$recommended = $client -> recommended;
					}
				}
			}

			if (!empty($row -> client_id)) {
				foreach ($client_data as $key => $value) {
					//如果一樣表示有這個值
					if ($key == $row -> client_id) {
						$client_s_name = $client_data[$row -> client_id];
					}
				}
			}

			$object_usage_title = '';
			if (!empty($row -> object_usage)) {
				$object_usage_title=''; //= $object_usage_array[$row -> object_usage];
			}
			$type = '';
			if ($row -> type_rent >= 1) {
				$type = '租';
			}

			if ($row -> type_sell >= 1) {
				$type = '售';
			}

			if ($row -> type_help_seal >= 1) {
				$type = '代銷';
			}
			if($this->competence_lib->competence($row , $all_se)){
				
				
				$entry = array('demand_id' => $row -> demand_id, 
					'cell' => array(
								'demand_id' => $row -> demand_id, 
								'name' => $client_s_name, 
								'recommended' => $recommended, 
								'home_address' => $row -> home_address, 
								'building_area1' => (int)$row -> building_area1 . "~" . (int)$row -> building_area2, 
								'indoor_area1' => (int)$row -> indoor_area1 . "~" . (int)$row -> indoor_area2, 
								'land_area1' => (int)$row -> land_area1 . "~" . (int)$row -> land_area2, 
								'sections1' => $row -> sections1, 
								'key_areas1' => $row -> key_areas1, 
								'circle1' => $row -> circle1, 
								'year_of_construction'=> $row -> year_of_construction,
								'total_budget1' => (int)$row -> total_budget1 . " ~ " . (int)$row -> total_budget2, 
								'broker' => $broker_name, 
								'object_usage' => $row -> object_usage, 
								'type_rent' => $type, 
								'ctime' => date("Y-m-d",$row -> ctime) , ), );
	
				$jsonData['rows'][] = $entry;
				
				
			}
		}
	
		echo json_encode($jsonData);
	}

	public function get_users() {

		$this -> load -> model("users_model");
		$users_ar = array();
		$reslut = $this -> users_model -> get_all_users();

		foreach ($reslut as $user) {
			$users_ar[$user -> users_id] = $user -> name;
		}

		return $users_ar;
	}

	//從資料庫找 all 聯絡人
	public function get_client_all_info() {
		//echo $id;

		$reslut = $this -> client_model -> get_all();
		return $reslut;
	}

	//從資料庫找  顧客資料／或是／聯絡人
	public function get_client_contact_data($type) {

		$result_data = array();

		if ($type == 'contact') {

			$contacts = $this -> contact -> get_all_contact();
			foreach ($contacts->result() as $contact) {
				$result_data[$contact -> client_contact_id] = $contact -> name;
			}
		}

		if ($type == 'client') {
			$clients = $this -> client_model -> get_all();
			foreach ($clients as $client) {
				$result_data[$client -> client_id] = $client -> name;
			}
		}

		return $result_data;
	}

}
